/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.googlecode.milib.animation.static_ {
	import com.googlecode.milib.util.MLArrayUtil;

	import flash.utils.Dictionary;

	public class ObjectAnimations {
	
		private var obj : Object;
		private var animationsElementsRunning : Object;
		private var animationsElements : Array;
		private static const servicedObjects : Dictionary = new Dictionary(true);
		private var animations:Array;
		
		public function ObjectAnimations(obj:Object) {
			this.obj=obj;
			animationsElementsRunning={};
			animationsElements=[];
			animations=[];
		}//<>
		
		public function setNewTargetByParam(param:String, target:Number):void {
			for(var i:uint=0,animationsElement:AnimationElement;i<animationsElements.length;i++){
				animationsElement=animationsElements[i];
				if(animationsElement.param==param){
					animationsElement.target=target;
				}
			}
		}
		
		public function regAnimationElement(ae:AnimationElement):void {
			animationsElements.push(ae);
		}
		
		public function regRunningAnimationElement(ae:AnimationElement):void {
			if(animationsElementsRunning[ae.param]){
				AnimationElement(animationsElementsRunning[ae.param]).terminateIfIsRunningInAnimation();
			}
			animationsElementsRunning[ae.param]=ae;
			ae.isBlocked=false;
		}
		
		public function unregRunningAnimationElement(ae:AnimationElement):void {
			for(var i:* in animationsElementsRunning){
				if(i==ae.param){
					delete animationsElementsRunning[ae.param];
				}
			}
		}
		
		public function regAnimation(anim:Animation):void {
			MLArrayUtil.addUnique(animations, anim);
		}
		
		public static function forInstance(obj:Object):ObjectAnimations {
			if(servicedObjects[obj]){
				return servicedObjects[obj];
			}else{
				servicedObjects[obj]=new ObjectAnimations(obj);
				return servicedObjects[obj];
			}
		}
		
	}
}